#include "plot3d.hpp"
#include <cassert>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[]) {
  //   Plot3dGrid grid;
  //   string in_grid_fname = "naca0012.bin";
  //   grid.readGridMetaData(in_grid_fname);
  //   grid.readGrid(in_grid_fname);
  //   grid.writeGrid("out_put.bin");

  //   vector<string> gname(2), bname(2);
  //   gname[0] = "cart.bin";
  //   gname[1] = "circular.bin";
  //   bname[0] = "cart_bc";
  //   bname[1] = "circular_bc";
  //   Plot3dGrid grids[2];
  //   GridBcIn bcin[2];

  //   for (int i = 0; i < 2; ++i) {
  //     grids[i].readGridMetaData(gname[i]);
  //     grids[i].readGrid(gname[i]);
  //     bcin[i].read(bname[i]);
  //   }

  //   Plot3dGrid outg;
  //   GridBcIn outb;
  //   combine(bcin, 2, outb, grids, 2, outg);
  //   outb.print("bc_out");
  //   outg.writeGrid("out_put.bin");
  if (argc != 2) {
    cerr << "./a.out  combine.json" << endl;
    exit(EXIT_FAILURE);
  }
  CombineOpts opt;
  initCombineOpts(argv[1], opt);
  if (opt.bc_fname.size() == 1) {
    if (opt.neighb_a2b.size() == 0) {
      cout << "num_bc_file ==1 do not need combine, neighb_a2b.size() == 0 do "
              "not need transform"
           << endl;
      return 0;
    } else {
      GridBcIn bcin;
      bcin.read(opt.bc_fname[0]);
      bcin.changeNeighbFromA2B(opt.neighb_a2b[0], opt.neighb_a2b[1]);
      return 0;
    }
  }

  std::vector<Plot3dGrid> grids(opt.grid_fname.size());
  std::vector<GridBcIn> bcin(opt.bc_fname.size());
  for (int i = 0; i < opt.grid_fname.size(); ++i) {
    grids[i].readGridMetaData(opt.grid_fname[i]);
    grids[i].readGrid(opt.grid_fname[i]);
    bcin[i].read(opt.bc_fname[i]);
  }
  GridBcIn out_bc;
  Plot3dGrid out_grid;
  combine(bcin.data(), bcin.size(), out_bc, grids.data(), grids.size(),
          out_grid);
  if (opt.neighb_a2b.size() == 2) {
    out_bc.changeNeighbFromA2B(opt.neighb_a2b[0], opt.neighb_a2b[1]);
  }

  out_bc.print(opt.out_bc_fname);
  out_grid.writeGrid(opt.out_grid_fname);
  return 0;
}